package com.abbas.smartCity.adapter

import android.widget.BaseAdapter
import android.view.ViewGroup
import android.view.LayoutInflater
import android.annotation.SuppressLint
import android.content.Context
import android.text.Html
import android.view.View
import android.widget.ImageView
import com.abbas.smartCity.R
import android.widget.TextView
import com.abbas.smartCity.bean.HomeNewsBean
import com.abbas.smartCity.bean.ServerBean
import com.abbas.smartCity.util.load

/**
 * 列表通用适配器
 * @param <T> 数据类型
 * @param <H> holder持有者
</H></T> */
class KListCommAdapter<T, H>(//数据
    private var data: MutableList<T>, //持有者类
    private val holderClazz: Class<H>, //将要使用的布局ID
    private val layoutId: Int
) : BaseAdapter() {
    override fun getCount(): Int {
        return data.size
    }

    override fun getItem(i: Int): T {
        return data[i]
    }

    override fun getItemId(i: Int): Long {
        return i.toLong()
    }

    override fun getView(i: Int, convertView: View?, viewGroup: ViewGroup): View {
        //没什么区别
        var view = convertView
        var holder: H? = null
        if (view == null) {
            try {
                holder = holderClazz.newInstance() //新建出 holder
                view = LayoutInflater.from(viewGroup.context)
                    .inflate(layoutId, viewGroup, false) //解析布局
                view.tag = holder //设置tag
            } catch (e: IllegalAccessException) {
                e.printStackTrace()
            } catch (e: InstantiationException) {
                e.printStackTrace()
            }
        } else {
            holder = view.tag as H //直接在tag当中拿出持有者
        }
        //根据布局绑定持有者
        onBindView(holder, view!!)
        //抽象方法，使用时重写决定
        show(data[i], view, holder, i, viewGroup.context)
        return view
    }

    /**
     * 根据布局将泛型持有者强转为对应的数据类型，我们可知，布局与持有者是一对一关系。所以转换不会出错
     * 布局与持有者是一对一关系
     * 数据类型与布局是多对一关系
     * @param holder 持有者
     * @param convertView layoutId view
     */
    private fun onBindView(holder: H?, convertView: View) {
        when (layoutId) {
            R.layout.server_item -> {
                val serverHolder = holder as ServerHolder?
                serverHolder!!.im = convertView.findViewById<View>(R.id.im) as ImageView
                serverHolder.tv = convertView.findViewById<View>(R.id.tv) as TextView
            }
            R.layout.news_item -> {
                val newsHolder = holder as NewsHolder?
                newsHolder!!.title = convertView.findViewById<View>(R.id.title) as TextView
                newsHolder.im = convertView.findViewById<View>(R.id.im) as ImageView
                newsHolder.sub = convertView.findViewById<View>(R.id.sub) as TextView
                newsHolder.tag1 = convertView.findViewById<View>(R.id.tag1) as TextView
                newsHolder.tag2 = convertView.findViewById<View>(R.id.tag2) as TextView
                newsHolder.tag3 = convertView.findViewById<View>(R.id.tag3) as TextView
                newsHolder.tag4 = convertView.findViewById<View>(R.id.tag4) as TextView
            }
            R.layout.youth_inn_gv_item -> {
                val youthInnGvHolder = holder as YouthInnGvHolder?
                youthInnGvHolder!!.im = convertView.findViewById<View>(R.id.im) as ImageView
            }
            R.layout.talent_policy_item -> {
                val talentPolicyHolder = holder as TalentPolicyHolder?
                talentPolicyHolder!!.name = convertView.findViewById<View>(R.id.name) as TextView
                talentPolicyHolder.sub = convertView.findViewById<View>(R.id.sub) as TextView
            }
        }
    }

    /**
     * 重置数据
     * @param data 数据
     */
    fun resetData(data: MutableList<T>) {
        this.data = data
        notifyDataSetChanged()
    }

    //添加数据
    fun append(data: List<T>?) {
        this.data.addAll(data!!)
        notifyDataSetChanged()
    }

    /**
     * 抽象方法，由使用场景具体决定
     * @param data 数据
     * @param convertView 视图
     * @param viewHolder 持有者
     * @param position 位置
     * @param context 上下文
     */
    @SuppressLint("NonConstantResourceId")
    protected fun show(
        data: T,
        convertView: View?,
        viewHolder: H?,
        position: Int,
        context: Context?
    ) {
        when (layoutId) {
            R.layout.server_item ->
                if (data is ServerBean.RowsBean) {
                val serverHolder = viewHolder as ServerHolder
                val serverData = data as ServerBean.RowsBean
                serverData.apply {
                    serverHolder.im?.load(imgUrl)
                    serverHolder.tv?.text = serviceName
                }
            }
            R.layout.news_item ->
                if (data is HomeNewsBean.RowsBean){
                val newsHolder = viewHolder as NewsHolder
                val newsData = data as HomeNewsBean.RowsBean
                newsData.apply {
                    newsHolder.im?.load(cover)
                    newsHolder.title?.text = title
                    newsHolder.sub?.text = Html.fromHtml(content)
                    newsHolder.tag1?.text = "KT IS MORE STRONGER"
                }
            }
        }
    }

    /**
     * 全部服务持有者
     */
    class ServerHolder {
        var im: ImageView? = null
        var tv: TextView? = null
    }

    /**
     * 新闻持有者
     */
    class NewsHolder {
        var title: TextView? = null
        var im: ImageView? = null
        var sub: TextView? = null
        var tag1: TextView? = null
        var tag2: TextView? = null
        var tag3: TextView? = null
        var tag4: TextView? = null
    }

    /**
     * 青年驿站区域列表持有者
     */
    class YouthInnGvHolder {
        var im: ImageView? = null
    }

    /**
     * 青年驿站不知道什么持有者
     */
    class TalentPolicyHolder {
        var name: TextView? = null
        var sub: TextView? = null
    }
}